package com.ruoyi.aop;

import com.ruoyi.common.annotation.ExecutionType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.log.service.ILogService;
import com.ruoyi.model.log.LogEntity;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 此类是正对于分类的增强
 *
 * @author 惜
 * */
@Aspect
@Component
@Slf4j
public class CategoryAOP {

    @Autowired
    private ILogService iLogService;
    /**
     * 对分类操作进行日志记录
     * */
//    @Before("execution(public * com.ruoyi.category.service.impl..*(..))")
    @After("execution(public * com.ruoyi.category.service.impl..*(..))")
    public void categoryLog(JoinPoint joinPoint) {
        // 得到请求对象
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        // 得到注解信息
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        ExecutionType annotation = AnnotationUtils.findAnnotation(signature.getMethod(), ExecutionType.class);

        // 如果没有得到此注解证明不需要打印日志
        if (annotation == null) {
            return;
        }

        // 执行方法名称
//        String methodName = joinPoint.getSignature().getName();
        String methodName = signature.getMethod().getName();

        // 只进行增删改的日志记录
        if (methodName.contains("query") || methodName.contains("select")) {
            return;
        }

        // 添加日志
        LogEntity logEntity = new LogEntity();

        logEntity.setLogType("0");
        logEntity.setCreateTime(DateUtils.getNowDate());
        logEntity.setExecutionName(methodName);
        logEntity.setExecutionPeople(IpUtils.getHostName()+"&"+request.getRemoteUser());
        logEntity.setExecutionIp(IpUtils.getIpAddr());
        // 添加注解内容
        logEntity.setExecutionType(annotation.value().getCode());

        int checked = iLogService.addLog(logEntity);
        log.warn(checked == 0 ? "添加日志失败" : "添加日志成功");


        // 日志信息
        StringBuffer sb = new StringBuffer();
        sb.append("在")
                .append(DateUtils.getDate())
                .append("时>>")
                .append("请求人为:")
                .append(IpUtils.getHostName())
                .append("&")
                .append(request.getRemoteUser())
                .append(">>ip地址为:")
                .append(IpUtils.getIpAddr())
                .append(">>进行了(  ")
                .append(methodName)
                .append("  )操作");
        // 打印日志信息
        log.info(sb.toString());
    }
}
